package com.eightbears.beboy.adapter;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.text.Spanned;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.eightbears.beboy.R;
import com.eightbears.beboy.base.util.CommonUtils;
import com.eightbears.beboy.base.util.GetCategory;
import com.eightbears.beboy.base.util.SPUtil;
import com.eightbears.beboy.base.view.TextViewExpandableAnimation;
import com.eightbears.beboy.base.view.image.view.ImageItem;
import com.eightbears.beboy.base.view.layout.WrappableGridLayoutManager;
import com.eightbears.beboy.entity.HomeListEntity;
import com.eightbears.beboy.entity.UserEntity;
import com.makeramen.roundedimageview.RoundedImageView;

import java.util.List;


/**
 * Created by Bao on 2016/12/7.
 * 首页适配器
 */
public class HomeListAdapter extends BaseQuickAdapter<HomeListEntity.Post, BaseViewHolder> {

    public static final String STATUS_BUY_CONTENT = "buyContent";
    public static final String STATUS_SHOW_CONTENT = "showContent";

    private TextView tv_like;
    private RecyclerView rv_img;
    private ImageView iv_shop_icon;
    private View view_pay;

    // 收费信息
    private RelativeLayout rl_by_tips;
    private LinearLayout ll_shop_info;
    private RelativeLayout rl_lock_layout;
    private ImageView iv_free_status;
    private ImageView iv_lock_status;
    private RelativeLayout rl_pay_bg;
    private TextView tv_buy_info;
    private TextView tv_shop_name;
    private TextView tv_shop_address;
    private TextView tv_shop_phone;

    private UserEntity mUserEntity;

    private OnClickImageItemListener mClickImageListener;
    private OnByContentClickListener mByContentClickListener;

    // 图片查看单击事件
    public interface OnClickImageItemListener {
        void onImageClick(BaseQuickAdapter baseViewHolder, View view, int i);
    }

    public void setOnImageClickListener(final OnClickImageItemListener clickListener) {
        this.mClickImageListener = clickListener;
    }

    // 购买帖子单击事件
    public interface OnByContentClickListener {
        void onByClick(BaseQuickAdapter baseQuickAdapter, View view, int i, String type);
    }

    public void setOnByContentClickListener(final OnByContentClickListener clickListener) {
        this.mByContentClickListener = clickListener;
    }

    public HomeListAdapter(Context context) {
        super(R.layout.item_home_list, null);
        this.mContext = context;
        if (null == mUserEntity)
            mUserEntity = SPUtil.getUser();
    }

    @Override
    protected void convert(final BaseViewHolder baseViewHolder, HomeListEntity.Post post) {

        // 设置基本数据
        initBaseData(baseViewHolder, post);

        // 复原View
        replaceView(baseViewHolder);

        // 图片数据
        showImages(baseViewHolder, post);

        // 显示购买信息
        showBuyInfo(baseViewHolder, post);
        // 赞
        if (post.getPraise() == 1) {
            Drawable drawable = mContext.getResources().getDrawable(R.mipmap.icon_zan_home_sel);
            drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
            tv_like.setCompoundDrawables(drawable, null, null, null);
        }
    }

    private void showBuyInfo(BaseViewHolder baseViewHolder, HomeListEntity.Post post) {
        // 判断条件，是否显示店铺信息内容
        if ((null != post.getShop() && !"".equals(post.getShop()))
                || (null != post.getAddress() && !"".equals(post.getAddress()))
                || (null != post.getContact() && !"".equals(post.getContact()))) {

            initBuyView(baseViewHolder);
            tv_shop_name.setText(post.getShop());
            tv_shop_address.setText(post.getAddress());
            tv_shop_phone.setText(post.getContact());

            //收费，免费信息
            if (post.getPrice() > 0) {
                rl_pay_bg.setBackgroundResource(R.color.bg_purchase_color);
                boolean isPurchase = post.getPurchase() != null && post.getPurchase().size() > 0;
                if (isPurchase || mUserEntity.getData().getVip_level() > 0
                        || mUserEntity.getData().getUser_id().equals(post.getUser_id())) {
                    showGoodsInfo(baseViewHolder);
                } else {
                    showBuyGoodsView(baseViewHolder, post);

                }
            } else {
                showFree(baseViewHolder);
            }
        }
    }


    private void initBuyView(BaseViewHolder baseViewHolder) {
        // 收费信息
        view_pay = baseViewHolder.getView(R.id.view_pay);
        rl_by_tips = (RelativeLayout) view_pay.findViewById(R.id.rl_by_tips);
        ll_shop_info = (LinearLayout) view_pay.findViewById(R.id.ll_shop_info);
        rl_lock_layout = (RelativeLayout) view_pay.findViewById(R.id.rl_lock_layout);
        iv_free_status = (ImageView) view_pay.findViewById(R.id.iv_free_status);
        iv_lock_status = (ImageView) view_pay.findViewById(R.id.iv_lock_status);
        rl_pay_bg = (RelativeLayout) view_pay.findViewById(R.id.rl_pay_bg);
        tv_buy_info = (TextView) view_pay.findViewById(R.id.tv_buy_info);
        tv_shop_name = (TextView) view_pay.findViewById(R.id.tv_shop_name);
        tv_shop_address = (TextView) view_pay.findViewById(R.id.tv_shop_address);
        tv_shop_phone = (TextView) view_pay.findViewById(R.id.tv_shop_phone);

    }

    // 显示图片
    private void showImages(BaseViewHolder baseViewHolder, HomeListEntity.Post post) {
        if (post.getImages() != null && post.getImages().size() > 0) {
            View imagesInfo = baseViewHolder.getView(R.id.view_image);
            rv_img = (RecyclerView) imagesInfo.findViewById(R.id.rv_img);
            imagesInfo.setVisibility(View.VISIBLE);
            iv_shop_icon = baseViewHolder.getView(R.id.iv_shop_icon);
            initImagesAdapter(post.getImages());
        }
    }


    /**
     * 设置基本信息
     */
    private void initBaseData(BaseViewHolder baseViewHolder, HomeListEntity.Post post) {
        ImageView iv_vip = baseViewHolder.getView(R.id.iv_vip);
        ImageView iv_level = baseViewHolder.getView(R.id.iv_level);
        CommonUtils.loadImageView(mContext, post.getUser().getCover(), R.mipmap.default_head,
                (RoundedImageView) baseViewHolder.getView(R.id.iv_user_head));
        baseViewHolder.setText(R.id.tv_nickname, post.getUser().getNickname())
                .setText(R.id.tv_publish_date, CommonUtils.TimeStamp2Date(post.getPosted_at(), "MM-dd HH:mm"))
                .setText(R.id.tv_category, "# " + GetCategory.getCategoryNameById(post.getCategory()))
                .setText(R.id.tv_like, post.getStatistic().getPraise())
                .setText(R.id.tv_comment, post.getStatistic().getComment())
                .setText(R.id.tv_repost, post.getStatistic().getRepost())
                .setText(R.id.tv_share, post.getStatistic().getShared())
                .addOnClickListener(R.id.iv_user_head)
                .addOnClickListener(R.id.ll_follow_drop_down)
                .addOnClickListener(R.id.ll_btn_like)
                .addOnClickListener(R.id.ll_btn_comment)
                .addOnClickListener(R.id.ll_btn_repost)
                .addOnClickListener(R.id.ll_btn_share);

        if (post.getUser().getVip_level() > 0)
            baseViewHolder.setVisible(R.id.iv_vip, true);
        else
            baseViewHolder.setVisible(R.id.iv_vip, false);

        if (post.getUser().getVip_level() == 1) {
            iv_vip.setVisibility(View.VISIBLE);
            iv_vip.setImageResource(R.mipmap.icon_new_vip1);
        } else if (post.getUser().getVip_level() == 2) {
            iv_vip.setVisibility(View.VISIBLE);
            iv_vip.setImageResource(R.mipmap.icon_new_vip2);
        } else {
            iv_vip.setVisibility(View.GONE);
        }

        if (post.getPoints() != null) {
            if (post.getPoints().getLevel() == 1) {
                iv_level.setImageResource(R.mipmap.icon_level_one);
            } else if (post.getPoints().getLevel() == 2) {
                iv_level.setImageResource(R.mipmap.icon_level_two);
            } else if (post.getPoints().getLevel() == 3) {
                iv_level.setImageResource(R.mipmap.icon_level_three);
            } else if (post.getPoints().getLevel() == 4) {
                iv_level.setImageResource(R.mipmap.icon_level_four);
            } else if (post.getPoints().getLevel() == 5) {
                iv_level.setImageResource(R.mipmap.icon_level_five);
            }
            iv_level.setVisibility(View.VISIBLE);
        }

        String content = post.getPost();
        TextViewExpandableAnimation contentText = baseViewHolder.getView(R.id.tv_content);
        contentText.setText(content);
        contentText.resetState(true);
    }

    /**
     * 初始化图片适配
     */
    private void initImagesAdapter(List<String> images) {
        ImagesAdapter mImagesAdapter = new ImagesAdapter();
        WrappableGridLayoutManager mWrapHeightGridLayoutManager = new WrappableGridLayoutManager(mContext, 3);
        rv_img.setLayoutManager(mWrapHeightGridLayoutManager);
        mImagesAdapter.setNewData(ImageItem.replaceImage(images));
        rv_img.setAdapter(mImagesAdapter);
        Glide.with(mContext).load(images.get(0)).crossFade().error(R.mipmap.default_icon).into(iv_shop_icon);
        mImagesAdapter.setOnItemChildClickListener(new OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                if (null != mClickImageListener)
                    mClickImageListener.onImageClick(adapter, view, position);
            }
        });
    }

    //图片适配
    private class ImagesAdapter extends BaseQuickAdapter<ImageItem, BaseViewHolder> {
        public ImagesAdapter() {
            super(R.layout.item_image, null);
        }

        @Override
        protected void convert(BaseViewHolder baseViewHolder, ImageItem image) {
            Glide.with(mContext).load(image.path).into((ImageView) baseViewHolder.getView(R.id.iv_item));
            baseViewHolder.addOnClickListener(R.id.iv_item);
        }
    }

    /**
     * 初始化View
     */
    private void replaceView(BaseViewHolder baseViewHolder) {
        baseViewHolder.setVisible(R.id.view_image, false);
        baseViewHolder.setVisible(R.id.view_repost, false);
        baseViewHolder.setVisible(R.id.view_pay, false);
        baseViewHolder.setVisible(R.id.ll_follow_drop_down, true);
        baseViewHolder.setVisible(R.id.ll_category, true);
        tv_like = baseViewHolder.getView(R.id.tv_like);
        Drawable drawable = mContext.getResources().getDrawable(R.mipmap.icon_zan_home);
        drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
        tv_like.setCompoundDrawables(drawable, null, null, null);
    }

    /**
     * 显示已购买信息
     */
    private void showGoodsInfo(final BaseViewHolder baseViewHolder) {
        view_pay.setVisibility(View.VISIBLE);
        rl_by_tips.setVisibility(View.GONE);
        ll_shop_info.setVisibility(View.VISIBLE);
        iv_free_status.setVisibility(View.GONE);
        rl_lock_layout.setVisibility(View.VISIBLE);
        rl_lock_layout.setBackgroundResource(R.color.text_color_ccc);
        iv_lock_status.setImageResource(R.mipmap.lock_on);

        ll_shop_info.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mByContentClickListener.onByClick(HomeListAdapter.this, ll_shop_info, baseViewHolder.getLayoutPosition() - 1, STATUS_SHOW_CONTENT);
            }
        });
    }

    /**
     * 显示购买视图
     */
    private void showBuyGoodsView(final BaseViewHolder baseViewHolder, HomeListEntity.Post post) {
        String buyExplain = mContext.getResources().getString(R.string.text_purchase_info);
        String formatExplain = String.format(buyExplain, post.getPrice());
        Spanned htmlExplain = Html.fromHtml(formatExplain);
        tv_buy_info.setText(htmlExplain);
        view_pay.setVisibility(View.VISIBLE);
        rl_by_tips.setVisibility(View.VISIBLE);
        ll_shop_info.setVisibility(View.GONE);
        iv_free_status.setVisibility(View.GONE);
        rl_lock_layout.setVisibility(View.VISIBLE);
        rl_lock_layout.setBackgroundResource(R.color.bg_luck_color);
        iv_lock_status.setImageResource(R.mipmap.lock);
        tv_buy_info.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mByContentClickListener.onByClick(HomeListAdapter.this, tv_buy_info, baseViewHolder.getLayoutPosition() - 1, STATUS_BUY_CONTENT);
            }
        });
    }

    /**
     * 显示免费信息，商家信息
     */
    private void showFree(final BaseViewHolder baseViewHolder) {
        view_pay.setVisibility(View.VISIBLE);
        // 如果是免费，则隐藏收费锁，显示免费图标
        iv_free_status.setVisibility(View.VISIBLE);
        rl_lock_layout.setVisibility(View.GONE);
        // 显示商家信息，隐藏购买提示
        rl_by_tips.setVisibility(View.GONE);
        ll_shop_info.setVisibility(View.VISIBLE);
        ll_shop_info.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mByContentClickListener.onByClick(HomeListAdapter.this, ll_shop_info, baseViewHolder.getLayoutPosition() - 1, STATUS_SHOW_CONTENT);
            }
        });
    }
}
